<?php


namespace App\Services\Result\Type;


use App\Models\Examination;
use Illuminate\Http\Request;
use App\Models\StudentResult;
use App\Services\Result\BaseResult;


class GradeResult extends BaseResult
{
    // 成绩分布统计
    public function show(Examination $testResult, Request $request)
    {
        parent::show($testResult,$request);
        $section_id = $request->input('section_id');

        // 本次考试下的试卷列表
        $data = StudentResult::query()
            ->select('id', 'user_id', 'name', 'username', 'fraction', 'all_num', 'userSectionIds', 'success_num', 'error_num', 'already_num', 'submit_type', 'join_time', 'test_list_id')
            ->where('examination_id', $this->examination_id)
            ->when($section_id, function ($query) use ($section_id) {
                $query->where('userSectionIds', 'like', '%,' . $section_id . ',%');
            })
            ->where('userSectionIds', '!=', ',')
            ->orderBy('fraction', 'desc')
            ->paginate($this->pageLimit());

        foreach ($data as $k => $item) {
            $data[$k]['not'] = $item['all_num'] - $item['already_num'];
        }

        return $data;
    }
}
